package com.mes.service.manu;

import com.mes.common.MesPdaConstant;
import com.yy.base.common.dao.Dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class CutJobService {
    @Autowired
    private Dao dao;

    //加载裁断作业信息
    public List<Map<String,Object>> selectCutJobList() {
        String sql = "SELECT " +
                "      e.*,c.name ProductName,f.Name FacilitiesName,t.Status exeStatus,n.Username Username,q.Name FLNAME,i.Data TLBS,k.name StatusName,date_part('day', t.exe_date_from - t.exe_date_to) EXEDATETOTAL from  " +
                "      Emeso_Job_Order e  " +
                "      LEFT JOIN M_Product c on e.M_Product_Id = c.M_Product_ID " +
                "      LEFT JOIN Emeso_Job_Order_Exe t on e.Emeso_Job_Order_Id = t.Emeso_Job_Order_Id " +
                "      LEFT JOIN Eam_Facilities f on f.Eam_Facilities_Id = t.Eam_Facilities_ID " +
                "      LEFT JOIN Emeso_ManuOrder g on e.Emeso_ManuOrder_Id = g.Emeso_ManuOrder_Id " +
                "      LEFT JOIN Emes_Pbom_Item h on h.Emes_Pbom_Id = g.Emes_Pbom_Id " +
                "      LEFT JOIN Emes_PBom_Item_Shape i on i.Emes_Pbom_Item_Id = h.Emes_Pbom_Item_Id " +
                "      LEFT JOIN Emeso_Job_Plan o on e.Emeso_ManuOrder_Id = o.Emeso_ManuOrder_Id " +
                "      LEFT JOIN Emeso_Job_Plan_Bom p on o.Emeso_Job_Plan_Id = p.Emeso_Job_Plan_Id " +
                "      LEFT JOIN Emes_Component q on q.Emes_Component_Id = p.Emes_Component_Id " +
                "      LEFT JOIN Ad_User n on t.Ad_User_ID = n.Ad_User_ID " +
                "      LEFT JOIN (select  " +
                "                       t.tablename, c.columnname, rl.value, rl.name, rl.seqno from ad_ref_list rl, ad_column c, ad_table t " +
                "                 where rl.ad_reference_id = c.ad_reference_value_id " +
                "                   and c.ad_table_id = t.ad_table_id " +
                "                   and upper(t.tablename) = upper('Emeso_Job_Order_Exe') " +
                "                   and upper(c.columnname) = upper('Status')) k on k.value = t.Status " +
                "      where t.Route_No = " + MesPdaConstant.CUTECODE;
        return dao.query(sql);


    }
    public List<Map<String,Object>> selectCutJobByCode(Map map) {
            String sql = "select a.*,b.NAME productName,c.name clName,h.Name colorName,t.status StatusVal,t.Emeso_Job_Order_Exe_Id,e.Name processName from Emeso_Job_Order_Exe t,Emeso_Job_Order a   " +
                "LEFT JOIN M_Product b on a.M_Product_Id = b.M_Product_ID " +
                "LEFT JOIN Emeso_Job_Plan g on a.Emeso_ManuOrder_Id = g.Emeso_ManuOrder_Id " +
                "LEFT JOIN Emeso_Job_Plan_Bom j on g.Emeso_Job_Plan_Id = j.Emeso_Job_Plan_Id " +
                "LEFT JOIN M_Product c on j.M_Product_Id = c.M_Product_ID " +
                "LEFT JOIN Emes_Color h on h.Emes_Color_Id = j.Emes_Color_Id " +
                "LEFT JOIN Emes_Process e on e.Emes_Process_Id = a.Emes_Process_Id " +
                "LEFT JOIN (select  " +
                "                       t.tablename, c.columnname, rl.value, rl.name, rl.seqno from ad_ref_list rl, ad_column c, ad_table t " +
                "                 where rl.ad_reference_id = c.ad_reference_value_id " +
                "                   and c.ad_table_id = t.ad_table_id " +
                "                   and upper(t.tablename) = upper('Emeso_Job_Order') " +
                "                   and upper(c.columnname) = upper('Status')) k on k.value = a.Status " +
                "where a.status not in('CANCEL','FINISHED','OK','CREATE') and a.Emeso_Job_Order_Id = t.Emeso_Job_Order_Id and t.Route_No= "+MesPdaConstant.CUTECODE+" and t.status not in('CANCEL','FINISHED','OK','CREATE') and a.code='";
        if(map.get("code")!= null && !"".equals(map.get("code"))){
            sql = sql+map.get("code")+"'";
            return dao.query(sql);
        }else{
            return null;
        }

    }
    public boolean updateCutJobData(Map map) {
        String sql = "update Emeso_Job_Order_Exe set status=? ,ad_user_id=?";
        boolean result = false;
        if(map.get("status")!= null && !"".equals(map.get("status"))){
            if("START".equals(map.get("status"))){
                sql = sql+ ",exe_date_from=now() ";
            }else{
                sql = sql+ ",exe_date_to=now() ";
            }
            if(map.get("emesoJobOrderExeId")!= null && !"".equals(map.get("emesoJobOrderExeId"))){
                sql = sql+ " where Emeso_Job_Order_Exe_Id =?";
                result = dao.executeSql(sql,map.get("status"),map.get("userId"),map.get("emesoJobOrderExeId")) > 0;

            }
        }
        return result;
    }
    public String geteUserId(String userCode) {
        String sql = "select ad_user_id from ad_user where username = ?";
        Map user = dao.queryUnique(sql, userCode);
        if (user != null) {
            return (String)user.get("ad_user_id");
        }
        return null;
    }
}
